<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产线或产线组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,NAME,LINE"
                                 placeholder="请选择产线或产线组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="产品" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partno">
              <j-popup
                v-model="model.partno"
                field="partno"
                org-fields="partno"
                dest-fields="partno"
                code="bom"
                :multi="true"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工站" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="stn">
              <j-search-select-tag v-model="model.stn" dict="mes_line_stn_cfg,STN,STN" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itempartno">
              <a-input v-model="model.itempartno" placeholder="请输入物料"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料版本" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemrev">
              <a-input v-model="model.itemrev" placeholder="请输入物料版本"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料批次起始数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qtyStart">
              <a-input-number v-model="model.qtyStart" placeholder="请输入物料批次起始数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lot">
              <a-input v-model="model.lot" placeholder="请输入物料批次"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="物料批次当前数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qty">
              <a-input-number v-model="model.qty" placeholder="请输入物料批次当前数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否不分产品共享物料:1是共享其他都不共享" :labelCol="labelCol"
                               :wrapperCol="wrapperCol"
                               prop="shared">
              <a-input v-model="model.shared" placeholder="请输入是否不分产品共享物料:1是共享其他都不共享"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="供应商批次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierLot">
              <a-input v-model="model.supplierLot" placeholder="请输入供应商批次"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="上料时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firsttime">
              <j-date placeholder="请选择上料时间" v-model="model.firsttime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="保留" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reserved">
              <a-input v-model="model.reserved" placeholder="请输入保留"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态. 0: 关闭, 1: 在线" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="status">
              <a-input v-model="model.status" placeholder="请输入状态. 0: 关闭, 1: 在线"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ctime">
              <j-date placeholder="请选择更新时间" v-model="model.ctime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

import { httpAction } from '@/api/manage'

export default {
  name: 'MesMaterialLotModal',
  components: {},
  data() {
    return {
      title: '操作',
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },

      confirmLoading: false,
      validatorRules: {
        line: [
          { required: true, message: '请输入产线或产线组!' }
        ],
        partno: [
          { required: true, message: '请输入产品!' }
        ],
        stn: [
          { required: true, message: '请输入工站!' }
        ],
        itempartno: [
          { required: true, message: '请输入物料!' }
        ],
        lot: [
          { required: true, message: '请输入物料批次!' }
        ],
        qty: [
          { required: true, message: '请输入物料批次当前数量!' }
        ],
        status: [
          { required: true, message: '请输入状态. 0: 关闭, 1: 在线!' }
        ]
      },
      url: {
        add: '/qis/mesMaterialLot/add',
        edit: '/qis/mesMaterialLot/edit'
      }

    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.$refs.form.clearValidate()
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
            that.close()
          })
        } else {
          return false
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    }

  }
}
</script>
